<?php

namespace app\api\server;

use app\admin\library\Auth as LibraryAuth;
use app\common\controller\Api;
use app\common\library\Auth;
use app\common\model\goods\Goods as GoodsGoods;
use app\common\model\goods\Spec;
use app\common\model\UserGroup;
use think\Controller;

/**
 * 商品逻辑
 */
class Goods extends Controller
{

    /**
     * 商品列表
     *
     */
    public static function goods_list($params)
    {
        extract($params);
        $goods = new GoodsGoods();
        if (!isset($type) || !$type) {
            exception('参数错误');
        }
        $goods->where('type', $type);
        $goods->where('goods.status','normal');
        if ($type == '2') {
            if ($meal != 'all') {
                $goods->where('meal', $meal);
            }
        }
        if (isset($keywords) && $keywords) {
            $goods->whereLike('title', '%' . $keywords . '%');
        }
        if (isset($hot) && in_array($hot, ['0', '1'])) {
            $goods->where('hot', $hot);
        }
        if (isset($category_id) && $category_id) {
            $goods->where('category_id', $category_id);
        }
        $lists = $goods->with(['group'])->order('weigh desc,sales desc')->paginate();
        foreach ($lists as $k => $v) {
            $v->vip_price = self::vip_price($v, $v->spec[0], 1);
        }
        return $lists;
    }

    //获取会员价格
    static  public function vip_price($goods, $spec, $num = 0)
    {
        $auth = Auth::instance();
        if ($goods == null) {
            return;
        }
        if ($spec == null) {
            return;
        }
        $total_fee = bcmul($spec->price, $num, 2);
        if ($goods->type == '2' && $goods->meal == '0' && $auth->group_id > 1) {
            switch ($auth->group_id) {
                case 2:
                    $vip_price =  $spec->primary_agent;
                    break;
                case 3:
                    $vip_price =  $spec->intermediate_agent;
                    break;
                case 4:
                    $vip_price =  $spec->advanced_agent;
                    break;
                case 5:
                    $vip_price =  $spec->county_agent;
                    break;
                case 6:
                    $vip_price =  $spec->city_agent;
                    break;
                case 7:
                    $vip_price =  $spec->province_agent;
                    break;
                case 8:
                    $vip_price =  $spec->first_sensible;
                    break;
                case 9:
                    $vip_price =  $spec->two_sensible;
                    break;
                case 10:
                    $vip_price =  $spec->three_sensible;
                    break;
                default:
                    $vip_price = $spec->price;
                    break;
            }
        } else {
            $vip_price = $spec->price;
        }
        $pay_fee = bcmul($vip_price, $num, 2);
        $discount_amount = bcsub($spec->price * $num, $pay_fee,  2);
        return compact('vip_price', 'discount_amount', 'pay_fee', 'total_fee');
    }

    /**
     * 增销减库
     * @param int $goods_id   商品id
     * @param int $spec_id   规格id
     * @param int $num 数量
     * @return bool
     */
    public static function reduceInventory($goods_id, $spec_id, $num)
    {

        try {
            $goods = GoodsGoods::get($goods_id);
            $spec = Spec::get($spec_id);
            if ($spec->stock < $num) {
                exception('库存不足');
            }
            $goods->setInc('sales', $num);
            $spec->setDec('stock', $num);
        } catch (\Exception $e) {
            exception($e->getMessage());
        }
        return true;
    }
    /**
     * 减销增库
     * @param int $goods_id   商品id
     * @param int $num 数量
     * @return bool
     */
    public static function stockUp($goods_id, $spec_id,  $num)
    {
        try {
            $goods = GoodsGoods::get($goods_id);
            $spec = Spec::get($spec_id);
            $goods->setDec('sales', $num);
            $spec->setInc('stock', $num);
        } catch (\Exception $e) {
            exception($e->getMessage());
        }
        return true;
    }
}
